﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UniRlv.Utility
{
    public class NetHelper
    {

        public static string GetLocalIp(bool bLast)
        {
            IPAddress localIp = null;

            try
            {
                IPAddress[] ipArray;
                ipArray = Dns.GetHostAddresses(Dns.GetHostName());

                if (bLast)
                {
                    localIp = ipArray.Last(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                }
                else
                {
                    localIp = ipArray.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                }


            }
            catch (Exception ex)
            {
            }
            if (localIp == null)
            {
                localIp = IPAddress.Parse("127.0.0.1");
            }
            return localIp.ToString();
        }

    }
}
